﻿/*
 * EndianConverter
 * 
 * initial implementation : 20.10.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Petra.Gis.DataServices;


namespace Petra.Gis.DataServices.SHP
{
  /// <summary>
  /// internal helper class that provides endian conversion for different endians
  /// regarding to tiff specification. intel = littleEndian, motorola = bigEndian
  /// </summary>
  internal static class EndianConverter
  {

    internal static DataEndian GetCurrentPlatformEndian()
    {
      if (BitConverter.IsLittleEndian)
        return DataEndian.LittleIndian;
      else
        return DataEndian.BigIndian;

    }




    internal static bool NeedEndianConversion(DataEndian dataEndian)
    {
      if ((dataEndian == DataEndian.BigIndian && BitConverter.IsLittleEndian) || (dataEndian == DataEndian.LittleIndian && !BitConverter.IsLittleEndian))
        return true;
      else
        return false;
    }




    internal static short ToInt16(short val, DataEndian dataEndian)
    {

      //check endian of current platform
      if (NeedEndianConversion(dataEndian))
      {
        byte[] b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return (short)BitConverter.ToInt16(b, 0);
      }
      else
      {
        return val;
      }
    }



    internal static ushort ToUInt16(ushort val, DataEndian dataEndian)
    {

      //check endian of current platform
      if (NeedEndianConversion(dataEndian))
      {
        byte[] b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return BitConverter.ToUInt16(b, 0);
      }
      else
      {
        return val;
      }
    }




    internal static int ToInt32(Int32 val, DataEndian dataEndian)
    {

      //check endian of current platform
      if (NeedEndianConversion(dataEndian))
      {
        byte[] b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return BitConverter.ToInt32(b, 0);
      }
      else
      {
        return val;
      }
    }



    internal static uint ToUInt32(UInt32 val, DataEndian dataEndian)
    {

      //check endian of current platform
      if (NeedEndianConversion(dataEndian))
      {
        byte[] b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return BitConverter.ToUInt32(b, 0);
      }
      else
      {
        return val;
      }
    }





    internal static double ToDouble(Double val, DataEndian dataEndian)
    {

      //check endian of current platform
      byte[] b;
      if (NeedEndianConversion(dataEndian))
      {
        b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return BitConverter.ToDouble(b, 0);
      }
      else
      {
        return val;
      }
    }




    internal static float ToFloat(float val, DataEndian dataEndian)
    {

      //check endian of current platform
      byte[] b;
      if (NeedEndianConversion(dataEndian))
      {
        b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return (float)BitConverter.ToSingle(b, 0);

      }
      else
      {
        return val;
      }
    }


    /*
    internal static byte[] ToArray(Double val, WKB.ByteOrder dataEndian)
    {

      //check endian of current platform
      byte[] b;
      if ((dataEndian == WKB.ByteOrder.BigIndian && BitConverter.IsLittleEndian) || (dataEndian == WKB.ByteOrder.LittleIndian && !BitConverter.IsLittleEndian))
      {
        b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return b;
      }
      else
      {
        return BitConverter.GetBytes(val);
      }
    }




    internal static byte[] ToArray(Int32 val, WKB.ByteOrder dataEndian)
    {

      //check endian of current platform
      byte[] b;
      if ((dataEndian == WKB.ByteOrder.BigIndian && BitConverter.IsLittleEndian) || (dataEndian == WKB.ByteOrder.LittleIndian && !BitConverter.IsLittleEndian))
      {
        b = BitConverter.GetBytes(val);
        Array.Reverse(b);
        return b;
      }
      else
      {
        return BitConverter.GetBytes(val);
      }
    }
     */
  }
}
